import 'package:app/util/StringUtils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import './slidActions.dart';

class ListContainer extends Column {
  ListContainer(
      {super.key,
        String? title,
        required int length,
        required List<Widget> Function(int index) children,
        List<SlidActions>? actions,
        bool Function(int index)? isActions,
        ScrollController? controller
      })
      : super(crossAxisAlignment: CrossAxisAlignment.start, children: [
          getTitle(title ?? ""),
          Expanded(
            child: ListView.builder(
              itemBuilder: (context, index) {
                return  getList(title??'', index, length, children, actions??[], isActions);
              },
              controller: controller,
              itemCount: length,
          )),
        ]);

  static List<SlidableAction> getActions(List<SlidActions> actions, int index){
      return actions.map((item) => SlidableAction(
          onPressed: (context)=>{ item.onPressed(index) },
          backgroundColor: item.backgroundColor?? Colors.red,
          foregroundColor: item.foregroundColor?? Colors.white,
          icon: item.icon,
          label: item.label??'',
      )).toList();
  }

  /// 获取标题
  static Widget getTitle(String title) {
    if (StringUtils.isNotNull(title)) {
      return Padding(
          padding: const EdgeInsets.only(top: 10, bottom: 10, left: 10),
          child: Container(
            alignment: Alignment.centerLeft,
            child: Text(title ?? '',
                style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.w600,
                    color: Color.fromARGB(255, 128, 128, 128))),
          ));
    }
    return Container();
  }

  static Widget getList(String title, int index, int length,
      List<Widget> Function(int index) children, List<SlidActions> actions, isActions) {
    return Padding(
        padding: EdgeInsets.only(
            left: 10,
            right: 10,
            top: StringUtils.isNull(title) ? 10 : (index == 0 ? 0 : 20),
            bottom: (index == length - 1 ? 20 : 0)),
        child: Container(
          decoration: BoxDecoration(
            color: Colors.white, // 背景颜色
            borderRadius: BorderRadius.circular(10.0), // 圆角的半径
          ),
          child: Slidable(
            key: Key('item${index}'),
            enabled: !actions.isEmpty && (StringUtils.isNotNull(isActions) ? isActions(index) : true),
            endActionPane: ActionPane(
              motion: const ScrollMotion(),
              children: getActions(actions, index),
            ),
            child: Column(
              children: children(index),
            ),
          ),
        )
    );
  }
}
